import Config from 'config';
import { HttpRequest, asyncLogin, app } from 'httpRequest';
import Ye from '_YeQuery';
import Yang from '_YangQuery';
import Lian from '_LianQuery';




let apiQuery = Object.assign({}, Yang, Ye, Lian);
console.log(apiQuery)

apiQuery.asyncLogin = asyncLogin;


apiQuery.getUserInfo = function (params) {
  return new Promise(function (resolve, reject) {
    HttpRequest(true, Config.api.getUserInfo, 1, params, "POST", resolve, reject)
  })
}

//用户信息授权
apiQuery.authUserInfo = function (params) {
  return new Promise(function (resolve, reject) {
    if (params.detail.errMsg.indexOf("deny") === -1) {
      let userInfo = app.getStorageByKey("userInfo");
      wx.getUserInfo({
        success: function (pres) {
          if (!userInfo.nickName) {
            asyncLogin(true).then(res => {
              apiQuery.getUserInfo({
                encryptedData: params.detail.encryptedData,
                iv: params.detail.iv
              }).then(res => {
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 1000
                })
                //设置本地存储
                app.globalData.userInfo = res.data;
                app.setStorage("userInfo", res.data, resolve, reject);
              })
            })
          } else {
            resolve(userInfo)
          }
        }
      })
    } else {
      wx.showToast({
        title: '您拒绝授权，将无法进行更多操作！',
        icon: 'none',
        duration: 2000
      });
      reject();
    }
  })
}

//用户手机号码授权
apiQuery.authUserPhone = function (params) {
  return new Promise(function (resolve, reject) {
    if (params.detail.errMsg.indexOf("deny") === -1) {
      let userInfo = app.getStorageByKey("userInfo");
      wx.getUserInfo({
        success: function (pres) {
          if (!userInfo.mobile) {
            asyncLogin(true).then(res => {
              apiQuery.getUserInfo({
                encryptedData: params.detail.encryptedData,
                iv: params.detail.iv
              }).then(res => {
                //设置本地存储
                app.globalData.userInfo = res.data;
                app.setStorage("userInfo", res.data, resolve, reject);
              })
            })
          } else {
            resolve(userInfo)
          }
        }
      })
    } else {
      wx.showToast({
        title: '您拒绝授权，将无法进行更多操作！',
        icon: 'none',
        duration: 2000
      });
      reject();
    }
  })
}





export default apiQuery;



